import struct
import sys
import bsddb

if __name__ == "__main__":
    projectname = 'wbCt100Sep08'
    bdbpath = projectname+'/db.webgraph.bdb'
    binpath = projectname+'/db.webgraph'

    db = bsddb.btopen(bdbpath,'c')
    totalnode = 0
    binfile = open(binpath,'rb')

    intsize = struct.calcsize('i')

    while 1:
        data = binfile.read(intsize)
        if data == '':
            break
        srcid = str(struct.unpack('i',data)[0])

        data = binfile.read(intsize)
        if data == '':
            break
        outdegree = struct.unpack('i',data)[0]

        destlist = []
        for i in range(outdegree):
            data = binfile.read(intsize)
            if data == '':
                break
            destid = str(struct.unpack('i',data)[0])
            destlist.append(destid)
        db[srcid] = ' '.join(destlist)
    
        totalnode += 1

    print 'totalnode = ',totalnode
    binfile.close()
    db.close()
